package anonymouschat.service;

import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import anonymouschat.utils.ConstantVariables;

    public class serverEntry implements Serializable {
		private static final long serialVersionUID = 4747248213859099964L;

		public serverEntry(String serverName, String serverIP, int serverPort, PublicKey publicKey)
    	{
    		System.out.println("Added server " + serverName + " at " + serverIP + ":" + serverPort + " to list of available servers");
    		this.serverName = serverName;
    		this.serverIP = serverIP;
    		this.serverPort = serverPort;
    		this.publicKey = publicKey;
    	}
    	public String serverName;
    	public int serverPort;
    	public String serverIP;
    	private PublicKey publicKey;
    	private Cipher AsymmetricCipherEngine = null;
    	private Cipher SymmetricCipherEngine = null;
    	private SecretKey SymmetricKey = null;
    	
    	public void setPublicKey(PublicKey pub) 
    	{
    		publicKey = pub;	
    	}
    	
    	public void setSymmetricKey(SecretKey sec)
    	{
    		SymmetricKey = sec;
    	}
    	
    	public Cipher getAsymmetricCipherEngine() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
    	{
    		if (AsymmetricCipherEngine == null)
    		{
    			Cipher enc = Cipher.getInstance(ConstantVariables.ASYMMETRIC_CYPHER_TYPE);
			    enc.init(Cipher.ENCRYPT_MODE, publicKey);
			    AsymmetricCipherEngine = enc;
    		}
    		return AsymmetricCipherEngine;
    	}
    	
    	public Cipher getSymmetricCipherEngine() throws Exception
    	{
    		if(!hasSymmetricKey())
    		{
    			throw new Exception("No Symmetric Key");
    		}
    		
    		Cipher enc = Cipher.getInstance(ConstantVariables.SYMMETRIC_CIPHER_TYPE);
    		enc.init(Cipher.ENCRYPT_MODE, SymmetricKey);
    		SymmetricCipherEngine = enc;
    		
			return SymmetricCipherEngine;
    		
    	}
    	
    	public boolean hasSymmetricKey()
    	{
    		if (SymmetricKey == null)
    			return false;
    		else
    			return true;
    	}
    }

